package com.example.mychat.Dao;

import com.example.mychat.Pojo.NewFriend;
import com.example.mychat.Pojo.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface userDao {
    @Insert("insert into user( password, name, logo,friend,phone,address) values (#{userInfo.password},#{userInfo.name},#{userInfo.logo},0,#{userInfo.phone},#{userInfo.address})")
    int userInsert(@Param("userInfo") UserInfo userInfo);
    /**
     * 好友删除--好友关系地图记录删除
     * @param sendId
     * @param toId
     * @return 返回数据库操作记录条数
     */
    @Delete("delete from chatdata_map where memberId like ('%'+#{toId}+'%') and memberId like ('%'+#{sendId}+'%')")
    int MapDelete(String sendId,String toId);

    /**
     * 好友删除--好友聊天数据删除
     * @param chatId 聊天数据的会话id
     * @return 返回数据库操作记录数
     */
    @Delete("delete from chat_for_person where chatid=#{chatId}")
    int deletefromperson(String chatId);

    @Insert("insert into addfriend(sendId, toId, state) values(#{newFriend.sendId},#{newFriend.toId},#{newFriend.state})")
    int New_friends(@Param("newFriend") NewFriend newFriend);

    @Update("update addfriend set state=#{newFriend.state} where sendId=#{newFriend.sendId} and toId=#{newFriend.toId}")
    int u_confirmFriendRequest(@Param("newFriend") NewFriend newFriend);


    //用concat将原id与新id拼接并用“，”分隔
    @Update("update user set friend=concat(friend,',',#{friendId} ) where id=#{myId}")
    int FriendAdd(@Param("myId") String myId,@Param("friendId") String friendId);

    @Update("update user set friend=trim(#{toId},from friend) where id=#{sendId}")
    int updateUser_friend(@Param("sendId") String sendId,@Param("toId") String toId);

    @Update("update user set password=#{newPassword} where id=#{id}")
    int resetPasswd(@Param("newPassword") String newPassword , @Param("id") String id );

    /**
     *
     * @param userid
     * @return 返回单个人的信息
     */
    @Select("select * from user where id=#{userid}")
    UserInfo selectUser(String userid);

    @Select("select id from user where phone=#{phone} or address=#{email}")
    String SelectUsernum(String phone,String email);

    /**
     *
     * @param myId
     * @return 返回多个人的信息的列表
     */
//    @Select("SELECT * FROM user WHERE id IN (SELECT friend FROM user WHERE id=#{myId})")
    @Select("select * from user where find_in_set(id,(select friend from user where id=#{myId}))")
    List<UserInfo> MyfriendInfoSelect(String myId);

    @Select("select id, name, logo, friend, phone, address from user where id=#{userInfo.id} and password=#{userInfo.password}")
    UserInfo Login_select(@Param("userInfo") UserInfo userInfo);

    @Select("select id, name, logo, friend, phone, address from user where phone=#{userInfo.phone} and password=#{userInfo.password}")
    UserInfo Login_selectPhone(@Param("userInfo") UserInfo userInfo);

    @Select("select id, name, logo, friend, phone, address from user where address=#{userInfo.address} and password=#{userInfo.password}")
    UserInfo Login_selectEmail(@Param("userInfo") UserInfo userInfo);

    /**
     * 查询好友验证列表
     * @param myId
     * @return
     */
    @Select("select name, id from addfriend,user where toId=#{myId} and addfriend.sendId=user.id and state=0")
    List<UserInfo> getmynewfriendlist(@Param("myId") String myId);

}
